package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.AreaInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.AreaInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.AreaInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.AreaInfoBean;
import cc.rengu.igas.bomp.facade.request.UnionAreaInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.UnionAreaInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 银联地区代码查询服务
 * Edit by 赵奎伍 on 20200331
 */
public class UnionAreaInfoQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        UnionAreaInfoQueryRequest unionAreaInfoQueryRequest = new UnionAreaInfoQueryRequest();
        ConvertUtil.convertOutput(unionAreaInfoQueryRequest);
        return unionAreaInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        UnionAreaInfoQueryRequest requestParams = (UnionAreaInfoQueryRequest) request;
        UnionAreaInfoQueryResponse responseParams = new UnionAreaInfoQueryResponse();
        BeanUtil.beanCopy(requestParams, responseParams);
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(responseParams);

        //region 查询字段
        String areaCode = requestParams.getAreaCode();//地区代码
        String areaName = requestParams.getAreaName();//地区名称
        int pageIndex = requestParams.getPageIndex();
        int pageSize = requestParams.getPageSize();
        rglog.debug("查询字段:areaCode<{}>,areaName<{}>,pageIndex<{}>,pageSize<{}>", areaCode, areaName, pageIndex, pageSize);
        //endregion

        /* 查询银联地区代码 */
        AreaInfoMapper areaInfoMapper = new AreaInfoMapperImpl();
        List<AreaInfo> areaInfoList = new ArrayList<>();
        if (-1 == pageIndex || -1 == pageSize) {
            //不分页
            areaInfoList = areaInfoMapper.selectAreaInfoByAreaCodeOrAreaName(areaCode, areaName);
            if (CollectionUtils.isEmpty(areaInfoList)) {
                rglog.info("根据查询条件查询到0条银联地区代码");
                responseParams.setTotalPage(BompAppParamConstant.INT_ZERO);
                responseParams.setTotalRows(BompAppParamConstant.INT_ZERO);
                responseParams.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                responseParams.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                return bizResponse;
            }
        } else {
            //分页查询
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(pageIndex);
            pageInfo.setPageSize(pageSize);
            PageResult pageResult = areaInfoMapper.selectAreaInfoPageByKeys(areaCode, areaName, pageInfo);
            if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult() || pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条银联地区代码");
                responseParams.setTotalPage(BompAppParamConstant.INT_ZERO);
                responseParams.setTotalRows(BompAppParamConstant.INT_ZERO);
                responseParams.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                responseParams.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                return bizResponse;
            }

            /* 根据取出记录条数做对应处理 */
            for (Object object : pageResult.getResult()) {
                areaInfoList.add((AreaInfo) object);
            }
            responseParams.setTotalPage(pageResult.getPageInfo().getTotalPage());
            responseParams.setTotalRows(pageResult.getPageInfo().getTotalRows());
        }

        rglog.info("SIZE=<{}>", areaInfoList.size());
        if (!areaInfoList.isEmpty()) {
            /* 按照创建时间降序排序 */
            //已在查询语句排序
            // List<AreaInfo> binInfoLists = areaInfoList.stream().sorted(Comparator.comparing(AreaInfo::getCreateTime)).collect(Collectors.toList());
            // Collections.reverse(binInfoLists);
            List<AreaInfoBean> areaInfoBeans = new ArrayList<>();
            areaInfoList.forEach(item -> convertAreaInfo(item, areaInfoBeans));
            responseParams.setAreaInfoList(areaInfoBeans);
        }

        /* 按照接口转换基础产品信息并响应给请求方 */
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        UnionAreaInfoQueryResponse responseParams = (UnionAreaInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(responseParams);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param areaInfo      数据库表结构对象
     * @param areaInfoBeans 接口对象
     */
    private void convertAreaInfo(AreaInfo areaInfo, List<AreaInfoBean> areaInfoBeans) {
        AreaInfoBean areaInfoBean = new AreaInfoBean();
        BeanUtil.beanCopy(areaInfo, areaInfoBean);
        areaInfoBeans.add(areaInfoBean);
    }
}
